
/**
 * Write a description of class Battle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Battle
{
    private Player player1;
    private Player player2;
    
    public Battle(Player p1, Player p2) {
        player1 = p1;
        player2 = p2;
    }
    
    private void fight(Player attacker, Player defender) {

        // if health is low, drink a potion
        if (defender.getHealth() < 5 && defender.getPotions() > 0) {
            System.out.println(defender.getName() + " drinks a potion.");
            defender.drinkPotion();
        }
            
        int damage = attacker.attack(defender);
            
        if (damage > 0) {
            System.out.println(attacker.getName() + " hits " 
                                + defender.getName() + " for " 
                                + damage + " points of damage");
        }
        else {
            System.out.println(attacker.getName() + " misses " + defender.getName());
        }

    }
    
    public void fight() {
    
        while (player1.isAlive() && player2.isAlive()) {
            fight(player1, player2);
            fight(player2, player1);
        }
        
        if (player1.isAlive()) {
            System.out.println(player1.getName() + " wins!");
        }
        else if (player2.isAlive()) {
            System.out.println(player2.getName() + " wins!");            
        }
        else {
            System.out.println("Everybody died!");                        
        }
    }
}
